package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanProjectService;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanProjectVo;
import com.zzyl.vo.NursingPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingPlanProjectService nursingPlanProjectService;

    /**
     * 新增护理计划
     * 操作2张表
     * 一次操作1张表的多条数据
     * 或者 一次操作多张表 希望同时成功 同时失败
     * 计划表
     * 中间表
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //Spring只能针对service层抛出的运行时异常进行回滚
    public void save(NursingPlanDto dto) {
//        对象属性拷贝 dto -> plan
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setStatus(1);
//        保存计划表,当前nursingPlan实体类中是没有主键id的
        nursingPlanMapper.insert(nursingPlan);
//        执行完成insert后,获取计划id
        Long planId = nursingPlan.getId();
        log.info("planId={}",planId);
//        批量保存中间表
        nursingPlanProjectService.saveBatch(dto.getProjectPlans(),planId);
    }

    /**
     * 条件分页查询
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> findByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
//       设置分页
        PageHelper.startPage(pageNum,pageSize);
//       分页查询
        List<NursingPlanVo> voList = nursingPlanMapper.selectByCondition(name,status);
//        分页的强转
        Page<NursingPlanVo> page = (Page<NursingPlanVo>) voList;
        return PageResponse.of(page,NursingPlanVo.class);

    }

    /**
     * 根据id查询
     * @param planId
     * @return
     */
    @Override
    public NursingPlanVo findById(Long planId) {
//        根据id 查询计划表
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(planId);
//        根据计划id 查询中间表
        List<NursingPlanProjectVo> list = nursingPlanProjectService.findByPlanId(planId);
//        把中间表数据放入计划表对象中
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 修改护理计划
     * @param dto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //Spring只能针对service层抛出的运行时异常进行回滚
    public void update(NursingPlanDto dto) {
//        属性拷贝
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
//        修改计划表
        nursingPlanMapper.updateById(nursingPlan);
        Long planId = dto.getId();
//        先根据计划表id 删除 中间表对应的所有数据
        nursingPlanProjectService.deleteByPlanId(planId);
//        新增中间表
        nursingPlanProjectService.saveBatch(dto.getProjectPlans(),planId);
    }

    /**
     * 删除护理计划
     * @param planId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //Spring只能针对service层抛出的运行时异常进行回滚
    public void delete(Long planId) {
//        删除计划表
        nursingPlanMapper.deleteById(planId);
//        根据计划id 删除中间表
        nursingPlanProjectService.deleteByPlanId(planId);
    }

    /**
     * 护理计划的启用和禁用
     * @param id
     * @param status
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class) //Spring只能针对service层抛出的运行时异常进行回滚
    public void updateStatus(Long id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateById(nursingPlan);
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> findByAll() {
        return nursingPlanMapper.selectAll();
    }
}
